package com.ruoyi.web.jobs;

import com.ruoyi.a2.domain.A2DynamicsInfo;
import com.ruoyi.a2.domain.A2Pdcc;
import com.ruoyi.a2.domain.A2StaticInfo;
import com.ruoyi.a2.domain.A2TargetPabc;
import com.ruoyi.a2.dto.A2DynamicsInfoCondition;
import com.ruoyi.a2.service.IA2DynamicsInfoService;
import com.ruoyi.a2.service.IA2StaticInfoService;
import com.ruoyi.a2.service.IA2TargetPabcService;
import com.ruoyi.common.enums.ActivityTypeEnum;
import com.ruoyi.common.enums.CleanStatusEnum;
import com.ruoyi.common.enums.CommonOKNOEnum;
import com.ruoyi.common.utils.Date2Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 目标活动提取.
 *
 * @author :yanlixiong
 * @date :2025-02-14 14:42:20
 */
@Configuration
@Slf4j
public class TargetActivityExtractionJob {

    @Autowired
    private IA2DynamicsInfoService a2DynamicsInfoService;

    @Autowired
    private IA2TargetPabcService a2TargetPabcService;

    @Autowired
    private IA2StaticInfoService a2StaticInfoService;


    /**
     * 目标活动提取
     * <p>
     * 上一次任务结束后等待指定时间执行下次任务；
     * 例如：每隔3秒执行定时任务，0秒开始第一次执行，任务执行花费5秒，则第二次任务执行开始时间是第 0+5+3=8 秒.
     *
     * @author :yanlixiong
     * @date :2025-02-14 14:43:33
     */
    //@Scheduled(fixedDelayString = "${a2.jobs.targetActivityExtractionJob}")
    public void handle() {
        //log.info("开始目标活动提取......");
        try {
            //查询【enableStatus=1】的AIS/ADS-B静态信息
            A2StaticInfo a2StaticInfo = new A2StaticInfo();
            a2StaticInfo.setEnableStatus(CommonOKNOEnum.OK.getCode());
            List<A2StaticInfo> a2StaticInfoList = a2StaticInfoService.selectA2StaticInfoList(a2StaticInfo);
            if(!CollectionUtils.isEmpty(a2StaticInfoList)){
                for(A2StaticInfo a2StaticInfoDB:a2StaticInfoList){
                    String mmsiIcao = a2StaticInfoDB.getMmsiIcao();
                    Date startTime = a2StaticInfoDB.getStartTime();
                    Date endTime = a2StaticInfoDB.getEndTime();
                    //查询【是否已更改状态=0】的数据
                    A2DynamicsInfoCondition condition = new A2DynamicsInfoCondition();
                    condition.setUpdateStatus(CommonOKNOEnum.NO.getCode());
                    condition.setTargetNum(mmsiIcao);
                    if(startTime!=null){
                        condition.setStartTime(Date2Utils.formatDate(startTime,Date2Utils.parsePatterns[1]));
                    }
                    if(endTime!=null){
                        condition.setEndTime(Date2Utils.formatDate(endTime,Date2Utils.parsePatterns[1]));
                    }
                    condition.setOrderByColumn("base_time");
                    List<A2DynamicsInfo> a2DynamicsInfoList = a2DynamicsInfoService.getList(condition);
                    if(!CollectionUtils.isEmpty(a2DynamicsInfoList)){
                        //查询目标异常行为配置表
                        List<A2TargetPabc> targetPabcList = a2TargetPabcService.getListByEnableOK();
                        if(!CollectionUtils.isEmpty(targetPabcList)) {
                            List<A2TargetPabc> targetPabcList4 = targetPabcList.stream().filter(v -> ActivityTypeEnum.DATA_4.getCode().equals(v.getAbnormalBehavior())).collect(Collectors.toList());
                            List<A2TargetPabc> targetPabcList5 = targetPabcList.stream().filter(v -> ActivityTypeEnum.DATA_5.getCode().equals(v.getAbnormalBehavior())).collect(Collectors.toList());
                            List<A2TargetPabc> targetPabcList6 = targetPabcList.stream().filter(v -> ActivityTypeEnum.DATA_6.getCode().equals(v.getAbnormalBehavior())).collect(Collectors.toList());
                            List<A2TargetPabc> targetPabcList7 = targetPabcList.stream().filter(v -> ActivityTypeEnum.DATA_7.getCode().equals(v.getAbnormalBehavior())).collect(Collectors.toList());
                            List<A2TargetPabc> targetPabcList8 = targetPabcList.stream().filter(v -> ActivityTypeEnum.DATA_8.getCode().equals(v.getAbnormalBehavior())).collect(Collectors.toList());

                            int size = a2DynamicsInfoList.size();

                            for (int i = 0; i <size ; i++) {
                                int indexBefore = i>0?i-1:i;
                                A2DynamicsInfo infoBefore = a2DynamicsInfoList.get(indexBefore);
                                A2DynamicsInfo info = a2DynamicsInfoList.get(i);

                                Integer status = info.getStatus();
                                //跳过【进港】、【离港】行为
                                if(!ActivityTypeEnum.DATA_1.getCode().equals(status) && !ActivityTypeEnum.DATA_2.getCode().equals(status)){
                                    //停靠
                                    //当前点位的航速为null或者为0，则判定为停靠
                                    if(!CollectionUtils.isEmpty(targetPabcList4)){
                                        Double speed = info.getSpeed();
                                        if(speed==null || speed==0) {
                                            info.setStatus(ActivityTypeEnum.DATA_4.getCode());
                                        }
                                    }
                                    //加速
                                    if(!CollectionUtils.isEmpty(targetPabcList5)){
                                        double threshold = targetPabcList5.get(0).getThreshold()/100.0;
                                        double speedDifference = info.getSpeed()-infoBefore.getSpeed();
                                        double baseTimeDifference = (info.getBaseTime().getTime()-infoBefore.getBaseTime().getTime())/60.0/1000.0;
                                        double sb = speedDifference/baseTimeDifference;
                                        if(sb>threshold){
                                            info.setStatus(ActivityTypeEnum.DATA_5.getCode());
                                        }
                                    }
                                    //减速
                                    if(!CollectionUtils.isEmpty(targetPabcList6)){
                                        double threshold = targetPabcList6.get(0).getThreshold()/100.0;
                                        double speedDifference = info.getSpeed()-infoBefore.getSpeed();
                                        double baseTimeDifference = (info.getBaseTime().getTime()-infoBefore.getBaseTime().getTime())/60.0/1000.0;
                                        double sb = speedDifference/baseTimeDifference;
                                        if(sb<-threshold){
                                            info.setStatus(ActivityTypeEnum.DATA_6.getCode());
                                        }
                                    }
                                    //失联
                                    if(!CollectionUtils.isEmpty(targetPabcList7)){
                                        //分钟换算成毫秒
                                        Integer intervalTime = targetPabcList7.get(0).getIntervalTime()*60*1000;
                                        long baseTimeDifference = info.getBaseTime().getTime()-infoBefore.getBaseTime().getTime();
                                        if(baseTimeDifference>intervalTime){
                                            info.setStatus(ActivityTypeEnum.DATA_7.getCode());
                                        }
                                    }
                                    //追踪中断
                                    if(!CollectionUtils.isEmpty(targetPabcList8)){
                                        //分钟换算成毫秒
                                        Integer intervalTime = targetPabcList8.get(0).getIntervalTime()*60*1000;
                                        long baseTimeDifference = info.getBaseTime().getTime()-infoBefore.getBaseTime().getTime();
                                        if(baseTimeDifference>intervalTime){
                                            info.setStatus(ActivityTypeEnum.DATA_8.getCode());
                                        }
                                    }
                                }
                                info.setUpdateStatus(CommonOKNOEnum.OK.getCode());
                                a2DynamicsInfoService.updateA2DynamicsInfo(info);
                            }
                        }

                    }
                }

            }

        } catch (Exception e) {
            log.error("目标活动提取.error|{}",e);
        }
        //log.info("结束目标活动提取......");
    }


}
